/* Copyright 2012 Oliver Katz

    This file is part of Theta.

    Theta is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#include "bytecode.h"

namespace vm
{
	string bytecodeToString(int op)
	{
		switch(op)
		{
			case _null: return "null";
			case _add: return "add";
			case _sub: return "sub";
			case _mul: return "mul";
			case _div: return "div";
			case _mod: return "mod";
			case _and: return "and";
			case _or: return "or";
			case _xor: return "xor";
			case _neg: return "neg";
			case _lt: return "lt";
			case _le: return "le";
			case _gt: return "gt";
			case _ge: return "ge";
			case _eq: return "eq";
			case _neq: return "neq";
			case _fadd: return "fadd";
			case _fsub: return "fsub";
			case _fmul: return "fmul";
			case _fdiv: return "fdiv";
			case _flt: return "flt";
			case _fle: return "fle";
			case _fgt: return "fgt";
			case _fge: return "fge";
			case _feq: return "feq";
			case _fneq: return "fneq";
			case _br: return "br";
			case _push: return "push";
			case _pop: return "pop";
			case _call: return "call";
			case _al: return "al";
			case _fr: return "fr";
			default: return "";
		}
	}

	string registerToString(int r)
	{
		switch(r)
		{
			case _rret: return "rret";
			case _ra0: return "ra0";
			case _ra1: return "ra1";
			case _rt0: return "rt0";
			case _rt1: return "rt1";
			case _rt2: return "rt2";
			default: return "";
		}
	}

	BytecodeClause::BytecodeClause()
	{
		lastReg = _rret;
	}

	BytecodeClause::BytecodeClause(int lr)
	{
		lastReg = lr;
	}

	void BytecodeClause::write(int op, int a0, int a1)
	{
		bytecode tmp;
		tmp.op = op;
		tmp.a0 = a0;
		tmp.a1 = a1;
		tmp.f = "";
		code.push_back(tmp);
	}

	void BytecodeClause::write(int op, string f)
	{
		bytecode tmp;
		tmp.op = op;
		tmp.a0 = 0;
		tmp.a1 = 0;
		tmp.f = f;
		code.push_back(tmp);
	}

	vector<bytecode> BytecodeClause::read()
	{
		return code;
	}

	void BytecodeClause::append(BytecodeClause c)
	{
		code.insert(code.end(), c.code.begin(), c.code.end());
	}

	void BytecodeClause::setLast(int lr)
	{
		lastReg = lr;
	}
}